﻿Imports System.Data.SqlClient

Public Class adm_columnnews
    Inherits System.Web.UI.Page
    Private sSQL As String
    Private objrd As SqlDataReader = Nothing
    Private objrds As SqlDataReader = Nothing
    Private objfind As New GetData
    Private ps As New PreparedStatement(sSQL)
    Private pss As New PreparedStatement(sSQL)
    Private i As Integer
    Private pages As Integer

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (Session("admin_ID") <> Nothing And Session("admin_ID") <> "") Then




            Dim columnId, strErr, langFlag As String
            strErr = ""
            Dim strSql As String = ""
            Dim sum, sumdesc As Integer
            langFlag = Request("langflag")


            If Request("del") <> Nothing And Request("del") <> "" Then
                
                sSQL = "delete from Column_New where New_ID =@newId"
                ps.addParameter("newId", Request("newsId"))
                ps.strSql = sSQL
                objfind.es(ps, strErr)
            End If

            i = Request("page")
            If i = Nothing Or i = 0 Then
                i = 1
           
            End If
            columnId = Request("aid")
            strErr = ""
            sSQL = "select count(*) from Column_New where 1=1"
            If (Request("aid") <> Nothing And Request("aid") <> "") Then
                strSql = " and Column_ID =@columnId"
                If Request("aid") = "unclassified" Then
                    ps.addParameter("columnId", "")
                Else
                    ps.addParameter("columnId", columnId)
                End If

            End If
            ps.strSql = sSQL + strSql
            Dim conn As SqlConnection = objfind.getConn()

            objrd = objfind.dr(ps, strErr, conn, objrd)
            objrd.Read()
            sum = objrd.Item(0)
            pages = (sum \ 10) + 1
            If i >= pages Then
                i = pages
            ElseIf i <= 0 Then
                i = 1
            End If
            If (i = pages) Then
                sumdesc = sum
            Else
                sumdesc = i * 10
            End If
            totalPage.InnerText = i.ToString & "/" & pages.ToString()
            sSQL = "select top(" & i & "*10) * from Column_New where 1=1"
            sSQL += strSql
            sSQL += " and New_ID not in (select top (" & i - 1 & "*10) New_ID from Column_New where 1=1"
            sSQL += strSql
            sSQL += " order by New_ID) and New_ID not in (select top(" & sum - sumdesc & ") New_ID from Column_New where 1=1"
            sSQL += strSql
            sSQL += " order by New_ID desc) order by New_ID"
            ps.strSql = sSQL


            objrd = objfind.dr(ps, strErr, conn, objrd)
            purchaseList.InnerHtml = "<table width=""98%"" border=""0"" cellpadding=""0"" cellspacing=""1"" style=""margin-top:10px;"">" & _
                                         "<tr>" & _
                                             "<td width=""40%"" bgcolor=""#CCCCCC"">Title</td>" & _
                                             "<td width=""15%"" bgcolor=""#CCCCCC"">Column</td>" & _
                                             "<td width=""15%"" bgcolor=""#CCCCCC"">Create Time</td>" & _
                                             "<td width=""30"" bgcolor=""#CCCCCC"">Option</td>" & _
                                         "</tr>"
            Dim con As SqlConnection = objfind.getConn()

            While (objrd.Read())
                sSQL = "select Column_Name from Columns where Column_ID =@columnId"
                pss.addParameter("columnId", objrd("Column_ID"))
                pss.strSql = sSQL

                objrds = objfind.dr(pss, strErr, con, objrds)
                Dim cName As String
                If objrds.HasRows Then
                    objrds.Read()
                    cName = objrds("Column_Name")
                Else
                    cName = "未分类"
                End If

                purchaseList.InnerHtml += "<tr>" & _
                                            "<td bgcolor=""#F5F5F5""> " & objrd("title") & "</td>" & _
                                            "<td bgcolor=""#F5F5F5""> " & cName & "</td>" & _
                                            "<td bgcolor=""#F5F5F5""> " & objrd("create_date") & "</td>" & _
                                            "<td bgcolor=""#F5F5F5""> <a href=""adm_editcolumnnew.aspx?newsId=" & objrd("New_ID") & """ >edit</a>&nbsp&nbsp&nbsp&nbsp<a href=""adm_columnnews.aspx?newsId=" & objrd("New_ID") & "&aid=" & Request("aid") & "&langflag=" & langFlag & "&del=del"">delete</a>" & _
                                        "</tr>"
            End While
            objfind.closeConn(con)
            purchaseList.InnerHtml += "</table>"
            pageNav.InnerHtml = "<a href=""adm_columnnews.aspx?page=1" & "&aid=" & Request("aid") & "&langflag=" & langFlag & """>home &nbsp;</a>" & _
                                "<a href=""adm_columnnews.aspx?page=" & i - 1 & "&aid=" & Request("aid") & "&langflag=" & langFlag & """>previous page &nbsp;</a>" & _
                                "<a href=""adm_columnnews.aspx?page=" & i + 1 & "&aid=" & Request("aid") & "&langflag=" & langFlag & """>next page &nbsp;</a>" & _
                                "<a href=""adm_columnnews.aspx?page=" & pages & "&aid=" & Request("aid") & "&langflag=" & langFlag & """>end &nbsp;</a>"



            sSQL = "select * from Columns"
            If langFlag <> "" Then
                sSQL += " where lang_flag=@langFlag"
                ps.addParameter("langFlag", langFlag)
            End If
            ps.strSql = sSQL
            objrd = objfind.dr(ps, strErr, conn, objrd)

            While (objrd.Read())
                column.Items.Add(New ListItem(
                 objrd("Column_Name").ToString(), objrd("Column_ID").ToString()))
            End While
            column.Items.Add(New ListItem(
                 "未分类", "unclassified"))
            objfind.closeConn(conn)

            If Request("aid") <> Nothing Or Request("aid") <> "" Then

                column.SelectedValue = Request("aid")
            Else
                column.Text = ""
            End If
        Else
            Response.Redirect("../adm_login.aspx")
        End If
    End Sub


   
End Class